"""
Configuration Module
===================

Configuration management for different environments.
Supports development, production, and testing configurations.
"""

import os
from datetime import timedelta

class BaseConfig:
    """Base configuration class with common settings."""
    
    # Flask settings
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-secret-key-change-in-production'
    
    # Database settings
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_RECORD_QUERIES = True
    
    # JWT settings
    JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY') or 'jwt-secret-key-change-in-production'
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=24)
    JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=30)
    
    # Redis settings
    REDIS_HOST = os.environ.get('REDIS_HOST') or 'localhost'
    REDIS_PORT = int(os.environ.get('REDIS_PORT') or 6379)
    REDIS_DB = int(os.environ.get('REDIS_DB') or 0)
    REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD')
    
    # MySQL settings
    MYSQL_HOST = os.environ.get('MYSQL_HOST') or 'localhost'
    MYSQL_PORT = int(os.environ.get('MYSQL_PORT') or 3306)
    MYSQL_DATABASE = os.environ.get('MYSQL_DATABASE') or 'flask_enterprise'
    MYSQL_USERNAME = os.environ.get('MYSQL_USERNAME') or 'root'
    MYSQL_PASSWORD = os.environ.get('MYSQL_PASSWORD') or ''
    
    # Message Queue settings
    QUEUE_BACKEND = os.environ.get('QUEUE_BACKEND') or 'redis'
    QUEUE_BROKER_URL = os.environ.get('QUEUE_BROKER_URL') or 'redis://localhost:6379/1'
    QUEUE_RESULT_BACKEND = os.environ.get('QUEUE_RESULT_BACKEND') or 'redis://localhost:6379/2'
    
    # Rate limiting
    RATELIMIT_STORAGE_URL = os.environ.get('RATELIMIT_STORAGE_URL') or 'redis://localhost:6379/3'
    
    # File upload settings
    MAX_CONTENT_LENGTH = 16 * 1024 * 1024  # 16MB
    UPLOAD_FOLDER = 'uploads'
    
    # Pagination settings
    POSTS_PER_PAGE = 10
    MAX_PER_PAGE = 100
    
    # Security settings
    BCRYPT_LOG_ROUNDS = 12
    
    @staticmethod
    def init_app(app):
        """Initialize application with configuration."""
        pass

class DevelopmentConfig(BaseConfig):
    """Development environment configuration."""
    
    DEBUG = True
    TESTING = False
    
    # Database URI for development
    SQLALCHEMY_DATABASE_URI = (
        f"mysql+pymysql://{BaseConfig.MYSQL_USERNAME}:{BaseConfig.MYSQL_PASSWORD}"
        f"@{BaseConfig.MYSQL_HOST}:{BaseConfig.MYSQL_PORT}/{BaseConfig.MYSQL_DATABASE}"
    )
    
    # Development-specific settings
    CACHE_TYPE = 'simple'
    LOG_LEVEL = 'DEBUG'

class ProductionConfig(BaseConfig):
    """Production environment configuration."""
    
    DEBUG = False
    TESTING = False
    
    # Database URI for production (use environment variables)
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or (
        f"mysql+pymysql://{BaseConfig.MYSQL_USERNAME}:{BaseConfig.MYSQL_PASSWORD}"
        f"@{BaseConfig.MYSQL_HOST}:{BaseConfig.MYSQL_PORT}/{BaseConfig.MYSQL_DATABASE}"
    )
    
    # Production-specific settings
    CACHE_TYPE = 'redis'
    LOG_LEVEL = 'WARNING'
    
    # Enhanced security for production
    SESSION_COOKIE_SECURE = True
    SESSION_COOKIE_HTTPONLY = True
    SESSION_COOKIE_SAMESITE = 'Lax'

class TestingConfig(BaseConfig):
    """Testing environment configuration."""
    
    DEBUG = True
    TESTING = True
    
    # Use in-memory database for testing
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
    
    # Testing-specific settings
    WTF_CSRF_ENABLED = False
    CACHE_TYPE = 'null'
    LOG_LEVEL = 'DEBUG'

# Configuration mapping
config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'testing': TestingConfig,
    'default': DevelopmentConfig
}